package com.woniuxy.config;


import com.woniuxy.shiro.UserRealm;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;

import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * @author zhang
 * @create 2021-07-29-14:38
 */
@Slf4j
@Configuration
public class ShiroConfig {

    //创建自定义userRealm
    @Bean
    public UserRealm userRealm() {
        UserRealm userRealm=new UserRealm();
        //加入认证匹配
        userRealm.setCredentialsMatcher(credentialsMatcher());

        return userRealm;
    }
/*    //创建自定义adminRealm
    @Bean
    public AdminRealm adminRealm() {
        AdminRealm adminRealm=new AdminRealm();
        //加入认证匹配
        adminRealm.setCredentialsMatcher(credentialsMatcher());

        return adminRealm;
    }*/
    //  加密
    @Bean
    public HashedCredentialsMatcher credentialsMatcher(){
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //加密方式
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //加密次数
        hashedCredentialsMatcher.setHashIterations(1024);
        return hashedCredentialsMatcher;
    }


    //管理器
    @Bean("securityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        List<Realm> realms = new ArrayList<>();

        securityManager.setRealm(userRealm);

        return securityManager;
    }
    //
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        log.debug("shiroFilterFactoryBean");
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        /*
        anon:无需权限
        authc:认证才能
        user:记住我才能访问
        perms:权限
        role:拥有角色才能访问
         */
        //未认证时
        shiroFilterFactoryBean.setLoginUrl("/user/login");

        HashMap<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/user/ucenter", "authc");
        filterMap.put("/user/login", "anon");
        filterMap.put("/order/list", "authc");
        filterMap.put("/cart/list", "authc");
        filterMap.put("/user/collect", "authc");
//        filterMap.put("user/*", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }



/*    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        chainDefinition.addPathDefinition("/user/login", "anon");
        chainDefinition.addPathDefinition("/user/ucenter", "authc");


        return chainDefinition;
    }*/
    /**
     * 配置使用自定义认证器，可以实现多Realm认证，并且可以指定特定Realm处理特定类型的验证
     * @return
     */
//    @Bean
//    public CustomizedModularRealmAuthenticator customizedModularRealmAuthenticator(){
//        CustomizedModularRealmAuthenticator customizedModularRealmAuthenticator = new CustomizedModularRealmAuthenticator();
//        //配置认证策略，只要有一个Realm认证成功即可，并且返回所有认证成功信息
//        customizedModularRealmAuthenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
//        List<Realm> realms = new ArrayList<>();
//        realms.add(adminRealm());
//        realms.add(userRealm());
//        customizedModularRealmAuthenticator.setRealms(realms);
//        return  customizedModularRealmAuthenticator;
//    }

}
